<script setup lang="ts">
import { ref, watch } from "vue";
import { useComment } from "./commentHook";

// import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";

import { useRenderIcon } from "@/components/ReIcon/src/hooks";
export interface Props {
  modelValue: boolean;
  readonly id: number | null;
}
const props = defineProps<Props>();
const visible = ref(false);
const isEdit = ref(false);
defineOptions({
  name: "Comment"
});
const closeForm = () => {
  handleClose(() => {});
};
const emit = defineEmits(["update:modelValue", "refresh"]);
const handleClose = (done: () => void) => {
  emit("update:modelValue", false);
  done();
};

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  onSearch,
  resetForm,
  handleSizeChange,
  handleCurrentChange
} = useComment();
function init() {
  form.articleId = props.id;
  onSearch();
}
watch(
  () => props.modelValue,
  value => {
    visible.value = value;
    if (value) {
      isEdit.value = false;
      init();
    }
  }
);
const formRef = ref();
</script>
<template>
  <el-drawer
    v-model="visible"
    size="70%"
    title="评论列表"
    :before-close="handleClose"
  >
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="bg-bg_color w-[99/100]"
    >
      <el-form-item label="只看显示" prop="onlyVisible">
        <el-checkbox v-model="form.onlyVisible" size="large" />
      </el-form-item>
      <el-form-item label="是否需要审核" prop="name">
        <el-checkbox v-model="form.isNeedAudit" size="large" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <x-table-bar title="评论列表" @refresh="onSearch">
      <template v-slot="{ size, checkList }">
        <x-table
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          :data="dataList"
          :columns="columns"
          :checkList="checkList"
          :pagination="pagination"
          :paginationSmall="size === 'small' ? true : false"
          :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)'
          }"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              v-if="row.isNeedAudit"
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
            >
              审核
            </el-button>
          </template>
        </x-table>
      </template>
    </x-table-bar>

    <template #footer>
      <el-divider />
      <div style="flex: auto">
        <el-button @click="closeForm">关闭</el-button>
      </div>
    </template>
  </el-drawer>
</template>
